package xm.chengdu.com.beijing_211;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import Threads.BluetoothCommunicateThread2;
import adapter.ScanListActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 蓝牙连接选择
 */
public class LanYaLianJieActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.fuwuqi)
    TextView fuwuqi;
    @BindView(R.id.pic)
    RelativeLayout pic;
    @BindView(R.id.Email)
    TextView Email;
    @BindView(R.id.vio)
    RelativeLayout vio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lanyalianjie);

        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("finish")) {
                finish();
            }


        }
    }

    @OnClick({R.id.img_back, R.id.pic, R.id.vio})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.pic:
                //蓝牙连接
                startActivity(new Intent(LanYaLianJieActivity.this, ScanListActivity.class));

                break;
            case R.id.vio:
                boolean bluetoothEnabled = isBluetoothEnabled();
                if (bluetoothEnabled) {
                    Toast.makeText(this, "Bluetooth has been turned on!", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivity(intent);

                }


                break;
        }
    }

    /**
     * 21
     * 当前 Android 设备的 bluetooth 是否已经开启
     * 22
     * <p>
     * 23
     *
     * @return true：Bluetooth 已经开启 false：Bluetooth 未开启
     * 24
     */

    public static boolean isBluetoothEnabled()

    {

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter

                .getDefaultAdapter();


        if (bluetoothAdapter != null)

        {

            return bluetoothAdapter.isEnabled();

        }


        return false;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
